﻿using Report;
using System;
using System.Windows;
using System.Windows.Threading;
using Gui.Scan;

namespace Gui
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
            this.Exit += new ExitEventHandler(App_Exit);
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            //UI线程未捕获异常处理事件
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            //非UI线程未捕获异常处理事件
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Scanner.Start();
        }

        void App_Exit(object sender, ExitEventArgs e)
        {
            Scanner.Stop();
        }

        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                e.Handled = true;
                ReportUtil.ReportError(e.Exception);
                MessageBox.Show(e.Exception.Message);
            }
            catch (Exception ex)
            {
                ReportUtil.ReportError(ex);
                MessageBox.Show("UI线程发生致命错误！");
            }

        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception)
            {
                Exception exception = (Exception)e.ExceptionObject;
                ReportUtil.ReportError(exception);
            }
        }


    }
}